const {
    LinValidator,
    Rule
} = require('../../core/lin-validator-v2')

const {
    User
} = require('../models/user')

class periodicalAddValidator extends LinValidator {
    constructor() {
        super()
        this.type = [
            new Rule('isInt', '需要传入1-3', {
                min: 1,
                max: 3
            }),
        ]
        this.index = [
            new Rule('isInt', '需要传入整数')
        ]
    }
}

class RegisterValidator extends LinValidator { //注册
    constructor() {
        super()
        this.username = [
            new Rule('isLength', '用户名不符合长度规范', {
                min: 4,
                max: 32
            }),
        ]
        this.type = [
            new Rule('isInt', '需要传入1-3', {
                min: 1,
                max: 3
            }),
        ]
        this.password1 = [
            // 用户指定范围 123456 $^
            new Rule('isLength', '密码至少6个字符，最多32个字符', {
                min: 6,
                max: 32
            }),
            new Rule('matches', '密码不符合规范', '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]')

        ]
        this.password2 = this.password1
    }

    validatePassword(vals) {
        const psw1 = vals.body.password1
        const psw2 = vals.body.password2
        if (psw1 !== psw2) {
            throw new Error('两个密码必须相同')
        }
    }

    async validatePassword(vals) {
        const username = vals.body.username
        const user = await User.findOne({
            where: {
                username
            }
        })
        if (user) {
            throw new Error('用户名已存在')
        }
    }

}

class deRegisterValidator extends LinValidator { //删除
    constructor() {
        super()
        this.id = [
            new Rule('isInt', 'id毕传'),
        ]
    }
}

class TokenValidator extends LinValidator { //登录
    constructor() {
        //隐藏的错误
        // Java
        // JS Python 
        super()
        this.account = [
            new Rule('isLength', '不符合账号规则', {
                min: 1,
                max: 32
            })
        ]
        this.secret = [
            //    可选参数
            new Rule('isOptional'),
            new Rule('isLength', '至少6个字符', {
                min: 6,
                max: 128
            })
        ]

    }
}
class searchUserListValidator extends LinValidator { //用户列表
    constructor() {
        super()
        this.p = [
            new Rule('isLength', 'p不符合规则', {
                min: 1,
            }),
            new Rule('isOptional', '', '')
        ]
        this.type = [
            new Rule('isInt', 'type不符合规则'),
            new Rule('isOptional', '', '')
        ]
        this.count = [
            new Rule('isInt', 'count不符合规则', {
                min: 1,
            }),
            new Rule('isOptional', '', 60000)
        ]
        this.start = [
            //    可选参数
            new Rule('isOptional', '', 0),
            new Rule('isInt', 'start不符合规则', {
                min: 0,
            })
        ]

    }
}
class addInfoValidator extends LinValidator { //添加用户信息
    constructor() {
        super()
        this.auth = [
            new Rule('isInt', '需要传入整数'),
        ]
    }
    validateAuths(data) {
        const menu = data.body.menu;
        if (!Array.isArray(menu)) {
            return [false, 'menu必须为非空数组'];
        }
        return true;
    }
}

class rolesMenuValidator extends LinValidator { //添加角色
    constructor() {
        super()
        this.menu = [
            new Rule('isLength', '必传',{
                min:1,
            }),
        ]
        this.rolesType = [
            new Rule('isInt', '需要传入整数'),
        ]
    }
}

class NotEmptyValidator extends LinValidator {
    constructor() {
        super()
        this.token = [
            new Rule('isLength', '不允许为空', {
                min: 1
            })
        ]
    }
}

class addGoodsValidator extends LinValidator {
    constructor() {
        super()
        this.goodsName = [
            new Rule('isLength', '不允许为空', {
                min: 1
            })
        ]
        this.coding = [
            new Rule('isLength', '不允许为空', {
                min: 1
            })
        ]
        this.stockNum = [
            new Rule('isInt', '只能正整数', {
                min: 1
            })
        ]
        this.price = [
            new Rule('isFloat', '只能为数字', {
                min: 1
            })
        ]
    }
}
class idValidator extends LinValidator {
    constructor() {
        super()
        this.id = [
            new Rule('isLength', '不允许为空', {
                min: 1
            })
        ]
    }
}



module.exports = {
    periodicalAddValidator,
    TokenValidator,
    NotEmptyValidator,
    addInfoValidator,
    RegisterValidator,
    searchUserListValidator,
    deRegisterValidator,
    rolesMenuValidator,
    addGoodsValidator,
    idValidator,
}